package com.jdz.gxq.info.admin.project.service;

import java.util.List;
import java.util.Map;

import jp.terasoluna.fw.dao.QueryDAO;
import jp.terasoluna.fw.dao.UpdateDAO;

import com.jdz.gxq.info.admin.project.bo.Project;
import com.jdz.gxq.info.admin.project.bo.TalkingProject;

public class TalkingProjectServiceImpl implements TalkingProjectService {
	
	private QueryDAO queryDAO = null;
	
	private UpdateDAO updateDAO = null;
	
	/**
	 * TalkingProject检索分页用，查询记录条数
	 * @return
	 */
	public int getTalkingProjectsCount(String projectName) {
		
		int totalCount = queryDAO.executeForObject("talkingproject.getTalkingProjectsCount",
				projectName, Integer.class);
		
		return totalCount;
	}
	
	/**
	 * TalkingProject检索
	 * @return
	 */
	public List<TalkingProject> getTalkingProjects(Map<String, Object> params) {
		
		return queryDAO.executeForObjectList("talkingproject.getTalkingProjects", params);
	}
	
	/**
	 * 追加TalkingProject
	 * @param talkingproject
	 */
	public void insertTalkingProject(TalkingProject talkingproject) {
		
		// 项目状态为:在谈
		talkingproject.setProjectStatus("1");
		talkingproject.setTalkingDate("2012-11-10");
		updateDAO.execute("talkingproject.insertProject", talkingproject);
		updateDAO.execute("talkingproject.insertTalkingProject", talkingproject);
	}
	
	/**
	 * 更新TalkingProject
	 * @param talkingproject
	 */
	public void updateTalkingProject(TalkingProject talkingproject) {
		
		updateDAO.execute("talkingproject.updateProject", talkingproject);
		updateDAO.execute("talkingproject.updateTalkingProject", talkingproject);
	}
	
	/**
	 * 通过id得到talkingproject
	 * @param id
	 * @return
	 */
	public TalkingProject getTalkingProjectById(int projectId) {

		TalkingProject talkingproject = queryDAO.executeForObject("talkingproject.getTalkingProjectById",
				projectId, TalkingProject.class);
		
		return talkingproject;
	}
	
	/**
	 * 删除TalkingProject
	 * @param talkingproject
	 */
	public void deleteTalkingProject(int projectId) {
		
		updateDAO.execute("talkingproject.deleteProjectById", projectId);
		
		updateDAO.execute("talkingproject.deleteTalkingProjectById", projectId);
	}
	
	/**
	 * 验证projectName是否重复
	 * flag: 1 追加时验证
	 * flag: 2 更新时验证
	 */
	public boolean checkTalkingProjectName(TalkingProject pTalkingProject, int flag) {
		
		Project project = queryDAO.executeForObject("talkingproject.getProjectByName",
				pTalkingProject.getProjectName(), Project.class);

		if (project == null) {
			return true;
		}
		
		// 更新验证，查出来的数据是不是本身
		if (flag == 2) {
			
			if (project.getProjectId() == pTalkingProject.getProjectId()) {
				return true;
			}
		}
		
		return false;
	}

	public void setQueryDAO(QueryDAO queryDAO) {
		this.queryDAO = queryDAO;
	}

	public void setUpdateDAO(UpdateDAO updateDAO) {
		this.updateDAO = updateDAO;
	}	
}
